/*
 * @Author: xushuhao
 * @Email: xshmouse@163.com
 * @Date: 2020-04-18 09:58:52
 * @Last Modified by: xushuhao
 * @Last Modified time: 2020-05-06 14:25:35
 * @Description: 手动路由，支持自定义过渡动画。手动传页面对象及参数。
 */

import 'package:flutter/material.dart';

class RouteTransition {
  Widget page;
  String trans;

  RouteTransition(Widget page, {String trans = 'fade'}) {
    this.page = page;
    this.trans = trans;
  }

  void pushTransRoute(BuildContext context) {
    Navigator.of(context).push(PageRouteBuilder(pageBuilder:
        (BuildContext context, Animation<double> animation,
            Animation<double> secondaryAnimation) {
      return page;
    }, transitionsBuilder: (
      BuildContext context,
      Animation<double> animation,
      Animation<double> secondaryAnimation,
      Widget child,
    ) {
      return returnTrans(trans, animation, child);
    }));
  }

  void pushAndRemoveUntilTransRoute(BuildContext context) {
    Navigator.of(context).pushAndRemoveUntil(
        PageRouteBuilder(pageBuilder: (BuildContext context,
            Animation<double> animation, Animation<double> secondaryAnimation) {
          return page;
        }, transitionsBuilder: (
          BuildContext context,
          Animation<double> animation,
          Animation<double> secondaryAnimation,
          Widget child,
        ) {
          return returnTrans(trans, animation, child);
        }),
        (Route<dynamic> rout) => false); //(route) => route == null
  }

  void pushReplacementTransRoute(BuildContext context) {
    Navigator.of(context).pushReplacement(PageRouteBuilder(pageBuilder:
        (BuildContext context, Animation<double> animation,
            Animation<double> secondaryAnimation) {
      return page;
    }, transitionsBuilder: (
      BuildContext context,
      Animation<double> animation,
      Animation<double> secondaryAnimation,
      Widget child,
    ) {
      return returnTrans(trans, animation, child);
    }));
  }

  ///无动画
  void pushReplacementRoute(BuildContext context) {
    Navigator.of(context).pushReplacement(PageRouteBuilder(
      pageBuilder: (BuildContext context, Animation<double> animation,
          Animation<double> secondaryAnimation) {
        return page;
      },
    ));
  }

  dynamic returnTrans(String trans, Animation<double> animation, Widget child) {
    switch (trans) {
      case 'fade':
        // 渐隐渐显
        return createFadeTransition(animation, child);
        break;
      case 'updown-slide':
        return createUpDownSlideTransition(animation, child);
        break;
      case 'downup-slide':
        return createDownUpSlideTransition(animation, child);
        break;
      case 'scale':
        return createScaleTransition(animation, child);
        break;
      case 'rotation':
        return createRotationTransition(animation, child);
        break;
      default:
        //右出左回
        return createFadeTransition(animation, child);
    }
  }

  /// 创建一个平移变换
  /// 跳转过去查看源代码，可以看到有各种各样定义好的变换
  FadeTransition createFadeTransition(
      Animation<double> animation, Widget child) {
    return new FadeTransition(
      opacity: new CurvedAnimation(
        parent: animation,
        curve: Curves.easeIn,
        reverseCurve: Curves.easeOut,
      ),
      child: child, // child is the value returned by pageBuilder
    );
  }

  SlideTransition createUpDownSlideTransition(
      Animation<double> animation, Widget child) {
    return new SlideTransition(
      position: new Tween<Offset>(
        begin: const Offset(0, -1), //从上到下  0，1 从下到上
        end: const Offset(0, 0),
      ).animate(animation),
      child: child, // child is the value returned by pageBuilder
    );
  }

  SlideTransition createDownUpSlideTransition(
      Animation<double> animation, Widget child) {
    return new SlideTransition(
      position: new Tween<Offset>(
        begin: const Offset(0, 1), //从上到下  0，1 从下到上
        end: const Offset(0, 0),
      ).animate(animation),
      child: child, // child is the value returned by pageBuilder
    );
  }

  // 缩放动画效果
  ScaleTransition createScaleTransition(
      Animation<double> animation, Widget child) {
    return ScaleTransition(
      scale: Tween(begin: 0.0, end: 1.0).animate(
          CurvedAnimation(parent: animation, curve: Curves.fastOutSlowIn)),
      child: child,
    );
  }

  // 旋转加缩放动画效果
  RotationTransition createRotationTransition(
      Animation<double> animation, Widget child) {
    return RotationTransition(
      turns: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
        parent: animation,
        curve: Curves.fastOutSlowIn,
      )),
      child: ScaleTransition(
        scale: Tween(begin: 0.0, end: 1.0).animate(
            CurvedAnimation(parent: animation, curve: Curves.fastOutSlowIn)),
        child: child,
      ),
    );
  }
}
